package com.sq.partner.manager.service.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import com.sq.partner.manager.mybatis.model.GatewayMenu;
import com.sq.partner.manager.mybatis.model.GatewayScanUnionMenu;
import com.sq.partner.manager.util.StrUtil;
import org.apache.axis.utils.StringUtils;
import org.springframework.stereotype.Service;

import com.sq.partner.manager.dao.UnionScanManagerDao;
import com.sq.partner.manager.mybatis.model.GatewayScanUnionCode;
import com.sq.partner.manager.service.UnionScanManagerService;
import com.sq.partner.manager.util.Page;
@Service
public class UnionScanManagerServiceImpl implements UnionScanManagerService{

	@Resource
	UnionScanManagerDao unionScanManagerDao;
	
	/* 
	 * @see com.sq.partner.manager.service.unionScanManagerService#queryUnionQRCodeListByPageNumber(com.sq.partner.manager.util.Page)
	 */
	
	@Override
	public Page<GatewayScanUnionCode> queryUnionQRCodeListByPageNumber(Page<GatewayScanUnionCode> page) throws Exception {
		
		page = unionScanManagerDao.queryUnionQRCodeListByPageNumber(page);
		
		return page ;
	}

	/* 
	 * SQL：
	 * @see com.sq.partner.manager.service.UnionScanManagerService#queryUnionCodeCountByConditions(com.sq.partner.manager.util.Page)
	 */
	@Override
	public int queryUnionCodeCountByConditions(Page<GatewayScanUnionCode> page) throws Exception {
		int count = unionScanManagerDao.queryUnionCodeCountByConditions(page);
		return count;
	}

	@Override
	public Page<Map<String, Object>> queryUnionCodeCanBeConfigByPageNumber(Page<Map<String, Object>> page) throws Exception {
		
		return unionScanManagerDao.queryUnionCodeCanBeConfigByPageNumber(page);
	}

	@Override
	public int queryUnionCodeCanBeCinfigCountByConditions(Page<Map<String, Object>> page) throws Exception {
		
		return unionScanManagerDao.queryUnionCodeCanBeCinfigCountByConditions(page);
	}

	@Override
	public Page<Map<String, Object>> queryMenuCanBeConfigByPageNumber(Page<Map<String, Object>> page) throws Exception {

		return unionScanManagerDao.queryMenuCanBeConfigByPageNumber(page);
	}

	@Override
	public int queryMenuCanBeCinfigCountByConditions(Page<Map<String, Object>> page) throws Exception {

		return unionScanManagerDao.queryMenuCanBeCinfigCountByConditions(page);
	}

	@Override
	public Page<Map<String, Object>> queryConfigedUnionCodeByPage(Page<Map<String, Object>> page) throws Exception {
		
		return unionScanManagerDao.queryConfigedUnionCodeByPage(page);
	}

	@Override
	public Page<Map<String, Object>> queryConfigedMenuByPage(Page<Map<String, Object>> page) throws Exception {

		return unionScanManagerDao.queryConfigedMenuByPage(page);
	}

	@Override
	public int queryConfigedUnionCodeCountByPage(Page<Map<String, Object>> page) throws Exception {
		
		return unionScanManagerDao.queryConfigedUnionCodeCountByPage(page);
	}

	@Override
	public int queryConfigedMenuCountByPage(Page<Map<String, Object>> page) throws Exception {

		return unionScanManagerDao.queryConfigedMenuCountByPage(page);
	}

	@Override
	public boolean deleteConfigedUnionCode(String id) throws Exception {
		 return unionScanManagerDao.deleteConfigedUnionCode(id);
	}

	@Override
	public boolean deleteMenuUnion(String id) throws Exception {
		return unionScanManagerDao.deleteMenuUnion(id);
	}

	@Override
	public boolean insertUnionCodeRela(String[] ids, String unionCodeId,String[] names,String menuId,String queryMenuLevel) throws Exception {
		
		return unionScanManagerDao.insertUnionCodeRela(ids, unionCodeId,names, menuId,queryMenuLevel);
		
	}

	@Override
	public boolean insertMenuRela(String[] ids, String menuId) throws Exception {

		return unionScanManagerDao.insertMenuRela(ids, menuId);

	}

	@Override
	public boolean checkBeforeAdd(String menuId, String type) throws Exception {

		String menuType = unionScanManagerDao.queryMenuType(menuId);

		if (StringUtils.isEmpty(menuType)){
			//添加菜单类型
			unionScanManagerDao.updateMenuType(menuId,type);

		}else if (! type.equals(menuType)){
			return false;
		}

		return true;
	}

	@Override
	public void checkAfterDelete(String menuId,String type) throws Exception {

		int count = 0;

		HashMap<String, Object> map = new HashMap();
		map.put("menuId",menuId);
//		map.put("ruleId",codeId);
		Page<Map<String, Object>> mapPage = new Page<Map<String, Object>>();
		mapPage.setMap(map);

		if ("P".equals(type)){
			//关联方案
			count = unionScanManagerDao.queryConfigedUnionCodeCountByPage(mapPage);
		}else if ("M".equals(type)){
			//关联子菜单
			count = unionScanManagerDao.queryConfigedMenuCountByPage(mapPage);
		}

		if (count == 0){
			//将置为null
			unionScanManagerDao.updateMenuType(menuId,"");
		}
	}

	@Override
	public int addNewUnionCode(Map<String, String> param) throws Exception {
		return unionScanManagerDao.addNewUnionCode(param);
	}

	@Override
	public int addNewMenu(Map<String, String> param) throws Exception {
		return unionScanManagerDao.addNewMenu(param);
	}

	@Override
	public GatewayScanUnionCode querySingleUnionCodeById(int id) throws Exception {
		return unionScanManagerDao.querySingleUnionCodeById(id);
	}

	@Override
	public boolean deleteUnionCode(String codeId) throws Exception {
		return unionScanManagerDao.deleteUnionCode(codeId);
	}

	@Override
	public boolean deleteMenu(String menuId) throws Exception {
		return unionScanManagerDao.deleteMenu(menuId);
	}

	@Override
	public Integer selectPlanCountByMenuId(String menuId) throws SQLException{
		return unionScanManagerDao.selectPlanCountByMenuId(menuId);
	}

	@Override
	public Page<GatewayScanUnionMenu> queryMenuListByPageNumber(Page<GatewayScanUnionMenu> page) throws Exception {
		page = unionScanManagerDao.queryMenuListByPageNumber(page);
		return page ;
	}

	@Override
	public int queryMenuListByConditions(Page<GatewayScanUnionMenu> page) throws Exception {
		int count = unionScanManagerDao.queryMenuListByConditions(page);
		return count;
	}

	@Override
	public boolean updateNewMenu(Map<String, String> param) throws Exception  {
		return unionScanManagerDao.updateNewMenu(param);
	}

	@Override
	public GatewayMenu querySingleMenuById(int codeId) throws Exception {
		return unionScanManagerDao.querySingleMenuById(codeId);
	}

	@Override
	public String queryMenuLevel(HashMap<String, Object> hashMap) throws SQLException {
		return unionScanManagerDao.queryMenuLevel(hashMap);
	}

	@Override
	public Integer selectPlanCountByUnionCode(String codeId) throws SQLException {
		return unionScanManagerDao.selectPlanCountByUnionCode(codeId);
	}

	@Override
	public Integer selectMenuCountByUnionCode(String codeId) throws SQLException {
		return unionScanManagerDao.selectMenuCountByUnionCode(codeId);
	}

	@Override
	public Integer selectSubMenuCountByMenuId(String menuId) throws SQLException{
		return unionScanManagerDao.selectSubMenuCountByMenuId(menuId);
	}

	@Override
	public boolean deleteMenuByUnionCode(String codeId) throws Exception {
		return unionScanManagerDao.deleteMenuByUnionCode(codeId);
	}

	@Override
	public boolean deleteUnionCodeByMenuId(String menuId) throws Exception {
		return unionScanManagerDao.deleteUnionCodeByMenuId(menuId);
	}
}
